package com.patMall.service.impl;

import com.patMall.entity.*;
import com.patMall.mapper.*;
import com.patMall.service.CommodityService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 750909637@qq.com
 * @PackageName patMall
 * @Package com.patMall.service.impl
 * @Date 2024/1/10 15:08
 * @Version 1.0
 */
@Service
public class CommodityServiceImpl implements CommodityService {
    @Resource
    private CommodityMapper commodityMapper;
    @Resource
    private CommodityImgMapper commodityImgMapper;
    @Resource
    private CommentMapper commentMapper;
    @Resource
    private CommentImgMapper commentImgMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private StoreMapper storeMapper;
    @Override
    public List<Commodity> selectAllByCommodity(int state) {
        List<Commodity> commodityList = commodityMapper.selectAllByCommodity(state);
        for (Commodity commodity:commodityList){
            List<CommodityImg> commodityImg = commodityImgMapper.getAllByCommodity_id(commodity.getCommodityId());
            commodity.setCommodityImgList(commodityImg);
            List<Comment> commentList = commentMapper.selectCommodityId(commodity.getCommodityId());
            commodity.setCommentList(commentList);
        }
        return commodityList;
    }

    @Override
    public List<Commodity> selectAllCommodity() {
        List<Commodity> commodityList = commodityMapper.selectAllCommodity();
        for (Commodity commodity:commodityList){
            List<CommodityImg> commodityImg = commodityImgMapper.getAllByCommodity_id(commodity.getCommodityId());
            commodity.setCommodityImgList(commodityImg);
            List<Comment> commentList = commentMapper.selectCommodityId(commodity.getCommodityId());
            commodity.setCommentList(commentList);
            Store store = storeMapper.selectByStoreId(commodity.getCommodityStoreId());
            commodity.setStore(store);
        }
        return commodityList;
    }

    @Override
    public Commodity getCommodityId(Integer commodityId) {
        Commodity commodity = commodityMapper.getCommodityId(commodityId);
            List<CommodityImg> commodityImg = commodityImgMapper.getAllByCommodity_id(commodityId);
            commodity.setCommodityImgList(commodityImg);
            List<Comment> commentList = commentMapper.selectCommodityId(commodityId);
            for (Comment comment : commentList){
                User user = userMapper.selectByPrimaryKey(comment.getUserId());
                comment.setUser(user);
                List<CommentImg> commentImgList = commentImgMapper.selectCommentImgList(comment.getCommentId());
                comment.setCommentImgList(commentImgList);
            }
        Store store = storeMapper.selectByStoreId(commodity.getCommodityStoreId());
        commodity.setStore(store);
            commodity.setCommentList(commentList);
        return commodity;
    }

    @Override
    public Commodity getCommodityByCommodityId(Commodity commodity) {
        return commodityMapper.getCommodityByCommodityId(commodity);
    }

    @Override
    public int addCommodity(Commodity commodity) {
        return commodityMapper.insertCommodity(commodity);
    }

    @Override
    public int updateCommodity(Commodity commodity) {
        return commodityMapper.updateCommodity(commodity);
    }

    @Override
    public int deleteCommodity(Integer commodityId) {
        return commodityMapper.deleteCommodity(commodityId);
    }

    @Override
    public List<Commodity> getAllByCommodityName(String commodityName) {
        List<Commodity> commodityList = commodityMapper.getAllByCommodityName(commodityName);
        for (Commodity commodity : commodityList) {
                List<CommodityImg> commodityImg = commodityImgMapper.getAllByCommodity_id(commodity.getCommodityId());
                commodity.setCommodityImgList(commodityImg);
                List<Comment> commentList = commentMapper.selectCommodityId(commodity.getCommodityId());
                commodity.setCommentList(commentList);
            }
            return commodityList;
        }

    @Override
    public List<Commodity> searchCommodity(Commodity commodity) {
        return commodityMapper.selectByCommodity(commodity);
    }

    @Override
    public List<Commodity> getStoreCommodityAll(Integer storeId, int sorting_p,int sorting_t) {
        List<Commodity> commodityList = commodityMapper.selectStoreCommodityAll(storeId, sorting_p, sorting_t);
        for (Commodity commodity : commodityList) {
            List<CommodityImg> commodityImg = commodityImgMapper.getAllByCommodity_id(commodity.getCommodityId());
            commodity.setCommodityImgList(commodityImg);
            List<Comment> commentList = commentMapper.selectCommodityId(commodity.getCommodityId());
            commodity.setCommentList(commentList);
        }
        return commodityList;
    }

    @Override
    public List<Commodity> getStoreCommodity() {
        List<Commodity> commodityList = commodityMapper.selectStoreCommodity();
        for (Commodity commodity:commodityList){
            List<CommodityImg> commodityImg = commodityImgMapper.getAllByCommodity_id(commodity.getCommodityId());
            commodity.setCommodityImgList(commodityImg);
            List<Comment> commentList = commentMapper.selectCommodityId(commodity.getCommodityId());
            commodity.setCommentList(commentList);
            Store store = storeMapper.selectByStoreId(commodity.getCommodityStoreId());
            commodity.setStore(store);
        }
        return commodityList;
    }
}
